package com.xing.controller;

import com.xing.DTO.LoginDTO;
import com.xing.DTO.SysUserDTO;
import com.xing.Utils.Result;
import com.xing.entity.SysUserEntity;
import com.xing.service.SysUserService;
import com.xing.utils.PageData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.extensions.Extension;
import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @version 1.0
 * @Author Xing
 * @Date 2023/11/19 10:17
 */
@Tag(name = "用户服务")
@RestController
@RequestMapping("user")
public class AdminController {
    @Autowired
    private SysUserService sysUserService;

    @Operation(summary = "测试接口")
    @GetMapping("test")
    public String test() {
        return "password";
    }

    @Operation(summary = "登陆")
    @PostMapping("login")
    public Result login(@RequestBody LoginDTO user) {
        return sysUserService.login(user);
    }

    @Operation(summary = "注册")
    @PostMapping("register")
    public Result register(@RequestBody LoginDTO user) {
        return sysUserService.register(user);
    }

    @GetMapping("getByUsername")
    @Operation(summary = "根据用户名获取信息")
    public Result getByUsername(@RequestParam("username") String username) {
        SysUserEntity user = sysUserService.getByUsername(username);
        return new Result().ok(user);
    }

    @GetMapping("/captcha")
    @Operation(summary = "图片验证码")
    public void getArticle(HttpServletResponse response, @Param("key") String key) {
        sysUserService.getArticle(response, key);
    }

    @GetMapping("/EmailCaptcha")
    @Operation(summary = "邮箱验证码", extensions = @Extension(properties = {
            @ExtensionProperty(name = "x-order", value = "3") // 设置顺序
    }))
    @Parameter(name = "email", description = "邮箱", required = true)
    public Result getArticle(@Param("email") String email) {
        try {
            sysUserService.getArticleByEmail(email);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result().err(501, "邮件发送失败");
        }
        return new Result().ok();
    }

    @GetMapping("info")
    @Operation(summary = "获取登陆信息")
    public Result getUserInfo() {
        return sysUserService.getUserInfo();
    }

    @GetMapping("{id}")
    @Operation(summary = "获取用户信息")
    public Result getUserById(@PathVariable("id") String id) {
        SysUserDTO dto = sysUserService.getUserById(id);
        return new Result().ok(dto);
    }

    @Operation(summary = "修改用户信息")
    @PutMapping("update")
    public Result update(@RequestBody SysUserEntity user) {
        boolean b = sysUserService.updateById(user);
        if (b) {
            return new Result().ok();
        }
        return new Result().err(303);
    }

    @Operation(summary = "分页获取用户信息")
    @GetMapping(value = "listByPage")
    @PreAuthorize("hasAuthority('admin')")
    public Result listByPage(@RequestParam("pageNum") Integer pageNum,
                             @RequestParam("pageSize") Integer pageSize,
                             @Param("username") String username) {
        PageData<SysUserDTO> userList = sysUserService.listByPage(pageNum, pageSize,username);
        return new Result().ok(userList);
    }

    @Operation(summary = "删除用户")
    @DeleteMapping(value = "{userId}")
    @PreAuthorize("hasAuthority('admin')")
    public Result delUser(@PathVariable("userId") Long userId) {
        Boolean result = sysUserService.delUser(userId);
        if (result) {
            return new Result().ok();
        }
        return new Result().err(610, "删除失败");
    }
}
